package com.douwong.student.popuwindow;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.douwong.student.R;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.Config;
import com.douwong.student.model.AppInfos;
import com.douwong.student.utils.PreferencesUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 家长控制
 * 
 * @author leihuajie
 *
 */
public class ParentControlPopu extends PopupWindow {

	private Context context;
	private View view;
	private GridView gridView;
	private ArrayList<AppInfos> list;
	private AppGVAdapter adapter;
	private LinearLayout parent_ll;
	private SharedPreferences preferences;
	private SharedPreferences.Editor editor;
	//private String[] name = { "系统桌面", "修改密码", "限时控制" };
	private String[] name = { "系统桌面", "修改密码" };
	//private int[] drawbs = { R.mipmap.system_d, R.mipmap.pwd_d, R.mipmap.time_d };
	private int[] drawbs = { R.mipmap.system_d, R.mipmap.pwd_d };

	public ParentControlPopu(final Context context) {
		this.context = context;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.from(context).inflate(R.layout.parent_control, null);
		parent_ll = (LinearLayout) view.findViewById(R.id.parent_ll);
		parent_ll.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		initData();
		gridView = (GridView) view.findViewById(R.id.parent_view);
		adapter = new AppGVAdapter();
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				switch (arg2) {
				// 系统桌面
				case 0:
					String[] infos=new String[]{"com.android.launcher", "com.android.launcher2.Launcher"};
					Intent intent = new Intent();
					ActivityInfo aiInfo=getLauncherActivityInfo(context);
					if(aiInfo!=null){
					  infos[0]=aiInfo.packageName;
					  infos[1]=aiInfo.name;
					}					
					intent.setComponent(new ComponentName(infos[0], infos[1]));
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					try {
						context.startActivity(intent);
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}	
					dismiss();
					break;
				// 修改密码
				case 1:
					pwdChange();
					break;
				// 限时控制
				case 2:
				//	timeControl();
					break;

				}
			}
		});

		this.setContentView(view);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0x4c000000);
		this.setBackgroundDrawable(dw);
	}

	void initData() {
		list = new ArrayList<AppInfos>();
		for (int i = 0; i < name.length; i++) {
			AppInfos aInfos = new AppInfos();
//			aInfos.appIcon = context.getResources().getDrawable(drawbs[i]);
			aInfos.appName = name[i];
			list.add(aInfos);
		}
	}
	
	/**
	 * 获取原生launcher 信息
	 * @param context
	 * @return
	 */
	
	 public ActivityInfo getLauncherActivityInfo(Context context) {
	        Intent intent = new Intent(Intent.ACTION_MAIN);
	        intent.addCategory(Intent.CATEGORY_HOME);
	        List<ResolveInfo> rls = context.getPackageManager().queryIntentActivities(intent, 0);
	        
	        ActivityInfo activityInfo=null;
	        
	        for(ResolveInfo rlf:rls){
	        	if(rlf.activityInfo.packageName.contains("com.android")){	        		
	        		activityInfo=rlf.activityInfo;
	        	}
	        }
	        
	        return activityInfo;	        
	    }

	class AppGVAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			convertView = LayoutInflater.from(context).inflate(R.layout.parent_control_item, null);
			TextView name = (TextView) convertView.findViewById(R.id.appgvitem_name);
			ImageView icon = (ImageView) convertView.findViewById(R.id.appgvitem_icon);
			AppInfos appInfo = list.get(position);
			name.setText(appInfo.appName);
			icon.setImageDrawable(context.getResources().getDrawable(drawbs[position]));
			return convertView;
		}

	}

	/**
	 * 修改密码
	 */
	private void pwdChange() {

		View view = LayoutInflater.from(context).inflate(R.layout.pwd_change, null);
		final EditText old_edit = (EditText) view.findViewById(R.id.old_edit);
		final EditText new_edit = (EditText) view.findViewById(R.id.new_edit);
		final EditText cnew_edit = (EditText) view.findViewById(R.id.cnew_edit);
		AlertDialog.Builder builde = new AlertDialog.Builder(context);
		builde.setTitle("密码修改");
		builde.setView(view);
		builde.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (old_edit.getText().toString().trim().equals(BaseApplication.AppKeyPWD)) {
					if (new_edit.getText().toString().trim().equals(cnew_edit.getText().toString().trim())) {
						Toast.makeText(context, "密码修改成功！", Toast.LENGTH_LONG).show();
						BaseApplication.AppKeyPWD = new_edit.getText().toString().trim();
						PreferencesUtils.getInstance().putString(Config.FAMILY_KEY_PWD, BaseApplication.AppKeyPWD);

					} else {
						Toast.makeText(context, "两次密码输入不正确！", Toast.LENGTH_LONG).show();
					}
				} else {
					Toast.makeText(context, "旧密码输入不正确！", Toast.LENGTH_LONG).show();
				}
			}
		});
		builde.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builde.show();
	}

/*	*//**
	 * 定时控制家长模式
	 *//*
	private void timeControl() {
		if (StaticVar.isTimeControl) {
			AlertDialog.Builder builde = new AlertDialog.Builder(context);
			builde.setItems(new String[]{"取消时间控制"}, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					StaticVar.isTimeControl=false;
					Intent intent = new Intent();
					intent.putExtra(ConfigInfo.TIME_CONTROL, false);
					intent.setAction(ConfigInfo.TIME_CONTROL);
					context.sendBroadcast(intent);
					ParentControlPopu.this.dismiss();
				}
			});
			builde.show();
		} else {
			View view = LayoutInflater.from(context).inflate(R.layout.time_control, null);
			final EditText hour_edit = (EditText) view.findViewById(R.id.hour_edit);
			final EditText minute_edit = (EditText) view.findViewById(R.id.minute_edit);
			AlertDialog.Builder builde = new AlertDialog.Builder(context);
			builde.setTitle("限时控制");
			builde.setView(view);
			builde.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					int hour = 0;
					int minute = 0;
					if (hour_edit.getText().toString().trim().equals("")) {
						hour = 0;
					} else {
						hour = Integer.valueOf(hour_edit.getText().toString().trim());
					}
					if (minute_edit.getText().toString().trim().equals("")) {
						minute = 0;
					} else {
						minute = Integer.valueOf(minute_edit.getText().toString().trim());
					}
					if(minute>59||minute<0){
						Toast.makeText(context, "时间格式不正确", Toast.LENGTH_LONG).show();
						return;
					}
					if (hour > 0 || minute > 0) {
						StaticVar.isTimeControl = true;
						Intent intent = new Intent();
						intent.putExtra(ConfigInfo.TIME_CONTROL, true);
						intent.putExtra(ConfigInfo.HOUR, hour);
						intent.putExtra(ConfigInfo.MINUTE, minute);
						intent.setAction(ConfigInfo.TIME_CONTROL);
						context.sendBroadcast(intent);
						ParentControlPopu.this.dismiss();
						
					}

				}
			});
			builde.setNegativeButton("取消", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			builde.show();
		}
	}*/
}
